import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Switch, Redirect } from 'react-router'
import { PrivateRoute } from 'components/route'
import { global as globalDuck, dataset as datasetDuck } from 'ducks'
import OrgList from './orgList'
import Dash from './dashboard'
import OrgContent from './orgContent'

class Org extends Component {
  componentDidMount = () => {
    const { actionSetActiveNavItem } = this.props
    actionSetActiveNavItem('org')
  }

  render() {
    const { location, match } = this.props
    return (
      <Switch location={location}>
        <PrivateRoute path={`${match.url}/`} component={OrgList} exact />
        <PrivateRoute path={`${match.url}/:orgName`} component={OrgContent} exact={false} />
        <Redirect from="*" to={`${match.url}/`} />
      </Switch>
    )
  }
}

const mapStateToProps = state => ({
  user: globalDuck.selectors.user(state),
})

const mapDispatchToProps = {
  actionSetActiveNavItem: globalDuck.creators.actionSetActiveNavItem,
  actionReciveDatasets: datasetDuck.creators.actionReciveDatasets,
}

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(Org)
